/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BarCode;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


 /*
 * @author Juan
 */
public class Imprimible extends JPanel implements Printable {
    
   public Imprimible() {}



public int print(Graphics g, PageFormat pf, int page) throws PrinterException {

if(page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}

/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;

g2d.translate(pf.getImageableX(), pf.getImageableY());

Image imagen=null;

try{
    
//imagen = Toolkit.getDefaultToolkit().getImage( "src/CodigosGenerados/Abc12345.png" );
    File a = new File(direccion);
    ImageIcon imageIcon=null;
    if(a.exists()){ imageIcon = new ImageIcon(a.getPath());
    }else {
        JOptionPane.showMessageDialog(null, "Error en la ruta del archivo a imprimir");
    }
   
   // "C://Users//Juan//Documents//NetBeansProjects//Alpha1//taller-de-proyectos-sistematizacion//ProyectoAlpha1//src//CodigosGenerados//Abc12345.png"
        
imagen = imageIcon.getImage(); 
if(imagen == null)
    System.out.print("no hay imagen");
//posicionamiento de la imagen
g.drawImage(imagen,300,300,this);

}catch(NullPointerException e){
    e.printStackTrace();
    JOptionPane.showMessageDialog(this, e);
}


return PAGE_EXISTS;

}
public String direccion;
public void imprimir(String direcc) {
    this.direccion=direcc;
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
ex.printStackTrace();
}
}
}
    
    
    
    
}